package cn.com.dao.impl;

import cn.com.dao.OrderDao;
import cn.com.dao.RoomDao;
import cn.com.domain.Order;
import cn.com.domain.Room;
import cn.com.utils.JDBCutils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public int findOrderTotalCount() {
        String sql="select count(*) from `order`";
        Connection conn= JDBCutils.getConnection();
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        int i=0;
        try {
            pstmt=conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            rs.next();
            i = rs.getInt(1);
            JDBCutils.close(rs,pstmt,conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;    }

    @Override
    public List<Order> findorderByPage(int start, int rows) {
        QueryRunner runner = new QueryRunner(JDBCutils.getDataSource());
        String sql="select * from `order` limit ? , ?";
        List<Order> orders=null;
        Object[] params={start,rows};
        try {
            orders = runner.query(sql, new BeanListHandler<Order>(Order.class),params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }

    @Override
    public Order findOrderbycustomer_name(String customer_name) {
        QueryRunner runner=new QueryRunner(JDBCutils.getDataSource());
        String sql="select * from `order` where `order_customer_name` = ?";
        Order order = null;
        try {
            order = runner.query(sql, new BeanHandler<>(Order.class), customer_name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }

    @Override
    public void delteOrderbyid(String ouid) {
        //删除订单
        QueryRunner runner =new QueryRunner(JDBCutils.getDataSource());
        String sql="select * from `order` where order_id = ?";
        Order order = null;
        try {
            order = runner.query(sql, new BeanHandler<>(Order.class), ouid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //使对应房间剩余数量加1
        RoomDao roomDao=new RoomDaoImpl();
        roomDao.alterRoomremainsrbyname(order.getOrder_room_name(),1);
        //删除
        sql="delete from `order` where order_id = ?";
        try {
            runner.update(sql,ouid);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void alterOrderbyid(Order order, String id) {
        QueryRunner runner =new QueryRunner(JDBCutils.getDataSource());
        String sql="UPDATE `order` SET `order_room_name`= ?, " +
                "`order_customer_name`=?, `order_days`=?, " +
                "`order_price`=?,`order_start` = ?, " +
                "`order_end` = ?,`order_ischeck` = ?" +
                "WHERE (`order_id`=?)";
        Object[] params={order.getOrder_room_name(),order.getOrder_customer_name(),
                order.getOrder_days(),order.getOrder_price(),order.getOrder_start(),
                order.getOrder_end(),order.getOrder_ischeck(),id};

        try {
            runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean addOrder(Order order) {
        RoomDao roomDao=new RoomDaoImpl();
        //通过名字找到剩余房间数量
        int remains = roomDao.findRoomremainsrbyname(order.getOrder_room_name());
        //剩余数量小于0就return
        if (remains<=0)
            return false;
        //使订单中的房间里剩余房间减1
        int i = roomDao.alterRoomremainsrbyname(order.getOrder_room_name());
        //添加一份订单
        QueryRunner runner =new QueryRunner(JDBCutils.getDataSource());
        String sql="insert into `order` values(?,?,?,?,?,?,?,?)";
        Object[] params={null,
            order.getOrder_room_name(),order.getOrder_customer_name(),
            order.getOrder_days(),order.getOrder_price(),
            order.getOrder_start(),order.getOrder_end(),
            order.getOrder_ischeck()};
        try {
            runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public double findpriceByorder_room_name(String order_room_name) {
        QueryRunner runner=new QueryRunner(JDBCutils.getDataSource());
        String sql="select * from `room` where room_name = ?";
        Room room=null;
        try {
            room= runner.query(sql, new BeanHandler<Room>(Room.class),order_room_name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (room!=null){
            return room.getRoom_price();
        }else
            return 0.0;
    }

    @Override
    public void delteOrderbyroomName(String roomname) {
        QueryRunner runner=new QueryRunner(JDBCutils.getDataSource());
        String sql="delete from `order` where order_room_name = ?";
        try {
            runner.update(sql,roomname);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void changIsChecked(String id, String ischeck) {
        QueryRunner runner=new QueryRunner(JDBCutils.getDataSource());
        String sql="UPDATE `order` SET `order_ischeck`= ? " +
                " WHERE (`order_id`=?)";
        try {
            runner.update(sql,ischeck,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
